package com.taiji.dianthus.dao.sms;

import com.taiji.dianthus.domain.sms.SmsGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @InterfaceName SmsGroupRepository
 * @Description
 * @Author H.M
 * @Date 2020/7/15
 */
@Repository
public interface SmsGroupRepository extends JpaRepository<SmsGroup, String>,
        JpaSpecificationExecutor<SmsGroup> {


    @Transactional
    @Modifying
    @Query("update SmsGroup t set t.enableFlag=0 where t.id=:id")
    void updateFlag(@Param("id") String id);

    @Query("select count(t) from SmsGroup t where t.enableFlag=1 and  t.groupName=:groupName and t.createUserId=:userId")
    Integer checkGroupName(@Param("groupName") String groupName, @Param("userId") String id);

    @Query("select count(t) from SmsGroup t where t.enableFlag=1 and  t.groupName=:groupName and t.createUserId=:userId and t.id<>:groupId")
    Integer checkEditGroupName(@Param("groupName") String groupName, @Param("groupId") String groupId, @Param("userId") String id);

    @Query("select t from SmsGroup t where t.enableFlag=1 and  t.createUserId=:userId")
    List<SmsGroup> getAllGroupNoPage(@Param("userId") String id);
}
